<template>
  <div>
    <el-table border style="width: 100%" :data="records">
      <el-table-column label="序号" width="80px" type="index" align="center">
      </el-table-column>
      <el-table-column prop="skuName" label="名称" width="150px">
      </el-table-column>
      <el-table-column prop="skuName" label="重量" width="150px">
      </el-table-column>
      <el-table-column prop="skuName" label="价格" width="150px">
      </el-table-column>
      <el-table-column align="center" label="默认图片" width="150px">
        <template slot-scope="{ row }">
          <img
            :src="row.skuDefaultImg"
            alt="默认图片"
            style="width: 80px; height: 80px"
          />
        </template>
      </el-table-column>
      <el-table-column prop="skuDefaultImg" label="描述" width="width">
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{ row }">
          <el-button
            @click="sale(row)"
            v-if="row.isSale == 0"
            type="success"
            icon="el-icon-bottom"
            size="mini"
          ></el-button>
          <el-button
            @click="cancel(row)"
            v-else
            type="primary"
            icon="el-icon-top"
            size="mini"
          ></el-button>
          <el-button
            type="warning"
            icon="el-icon-edit"
            @click="info"
            size="mini"
          ></el-button>
          <el-button
            type="info"
            icon="el-icon-info"
            @click="getSkuInfo(row)"
            size="mini"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!---->
    <el-pagination
      style="margin-top: 20px; text-align: center"
      @size-change="handleSizeChange"
      @current-change="getSkuList"
      :current-page="page"
      :page-sizes="[3, 5, 7, 9]"
      :page-size="limit"
      :pager-count="9"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
    >
    </el-pagination>
    <!--抽屉效果
      关闭 :before-close="handleClose"
      show-close:是否显示x关闭按钮
    -->
    <el-drawer
      title=""
      :visible.sync="drawer"
      :direction="direction"
      :show-close="false"
      size="50%"
    >
      <el-row>
        <el-col :span="5"
          ><div class="grid-content bg-purple">名称</div></el-col
        >
        <el-col :span="16"
          ><div class="grid-content bg-purple-light">
            {{ skuInfo.skuName }}
          </div></el-col
        >
      </el-row>
      <el-row>
        <el-col :span="5"
          ><div class="grid-content bg-purple">描述</div></el-col
        >
        <el-col :span="16"
          ><div class="grid-content bg-purple-light">
            {{ skuInfo.skuName }}
          </div></el-col
        >
      </el-row>
      <el-row>
        <el-col :span="5"
          ><div class="grid-content bg-purple">价格</div></el-col
        >
        <el-col :span="16"
          ><div class="grid-content bg-purple-light">
            {{ skuInfo.price }}元
          </div></el-col
        >
      </el-row>
      <el-row>
        <el-col :span="5"
          ><div class="grid-content bg-purple">平台属性</div></el-col
        >
        <el-col :span="16"
          ><div class="grid-content bg-purple-light">
            <template>
              <el-tag
                type="success"
                style="margin-right: 10px"
                v-for="attr in skuInfo.skuAttrValueList"
                :key="attr.id"
                >{{ attr.attrId }}-{{ attr.valueId }}</el-tag
              >
            </template>
          </div></el-col
        >
      </el-row>
      <el-row>
        <el-col :span="5"
          ><div class="grid-content bg-purple">商品图片</div></el-col
        >
        <el-col :span="16"
          ><div class="grid-content bg-purple-light">
            <el-carousel autoplay :interval="1000" loop type="card" height="300px">
              <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                <img :src="item.imgUrl" alt="" style="width:100%;height:100%">
              </el-carousel-item>
            </el-carousel>
          </div></el-col
        >
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      page: 1,
      limit: 3,
      total: 0,
      records: [],
      //存储sku信息
      skuInfo: {},
      drawer: false, //抽屉显示与隐藏
      direction: "rtl", //抽屉出现方向
    };
  },
  methods: {
    async getSkuInfo(row) {
      this.drawer = true;
      const result = await this.$API.sku.reqSkuById(row.id);
      if (result.code == 200) {
        this.skuInfo = result.data;
      }
    },
    info() {
      this.$message("正在开发中");
    },
    //上架
    async sale(row) {
      const result = await this.$API.sku.reqOnSale(row.id);
      if (result.code == 200) {
        this.$message.success("上架成功");
      }
      row.isSale = 1;
    },
    //下架
    async cancel(row) {
      const result = await this.$API.sku.reqDownSale(row.id);
      if (result.code == 200) {
        this.$message.success("下架成功");
      }
      row.isSale = 0;
    },
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSkuList();
    },

    //获取sku列表的方法
    async getSkuList(pager = 1) {
      const page = pager;
      const result = await this.$API.sku.reqGetSkuList(page, this.limit);
      if (result.code == 200) {
        this.limit = result.data.size;
        this.total = result.data.total;
        this.records = result.data.records;
      }
    },
  },
  mounted() {
    this.getSkuList();
  },
};
</script>
<style>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
  }
  
  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }
  
  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
  .el-carousel__button{
    width:10px;
    height:10px;
    background-color:red !important;
    border-radius: 50%;
  }
</style>
<style scoped>
.el-col-5 {
  font-size: 18px;
  text-align: right;
}
.el-col {
  margin: 5px;
}
</style>
